﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace IMIS_ERP.Web.Sale_Fee
{
    public partial class List_All : System.Web.UI.Page
    {

        IMIS_ERP.BLL.Sale_Fee bll = new IMIS_ERP.BLL.Sale_Fee();

        /// <summary>页面加载事件
        ///     <remarks>2010-12-08 河北大学信管团队</remarks>
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            lblMessage.Text = "";
            if (!IsPostBack)
            {
                ViewState["condition"] = "";
                if (Request.Params["msg"] != null && Request.Params["msg"].Trim() != "")
                {
                    if (Request.Params["msg"].ToString() == "a")
                    {
                        lblMessage.Text = "添加信息成功";
                    }
                    else if (Request.Params["msg"].ToString() == "u")
                    {
                        lblMessage.Text = "修改信息成功";
                    }
                }
                lbtnDel.Attributes.Add("onclick", "return confirm(\"你确认要删除吗？\")");
                Bind_GV();
            }
        }

        /// <summary>重写OnInit方法
        /// 	<remark>2010-12-08 河北大学信管团队 </remark>
        /// </summary>
        override protected void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Pagination1.NavigationClick += new ImageClickEventHandler(Pager_NavigationClick);
        }

        /// <summary>响应导航控件事件——重新绑定数据
        /// 	<remark>2010-12-08 河北大学信管团队 </remark>
        /// </summary>
        private void Pager_NavigationClick(object sender, ImageClickEventArgs e)
        {
            Bind_GV();
        }

        /// <summary>单击按钮事件：查询
        ///     <remarks>2010-12-08 河北大学信管团队</remarks>
        /// </summary>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            ViewState["condition"] = ddlSearch.SelectedValue + " like '%" + txtSearch.Text.Trim() + "%'";
            Bind_GV();
        }

        /// <summary>单击按钮事件：清空
        ///     <remarks>2010-12-08 河北大学信管团队</remarks>
        /// </summary>
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            txtSearch.Text = "";
        }

        /// <summary>单击按钮事件：删除
        ///     <remarks>2010-12-08 河北大学信管团队</remarks>
        /// </summary>
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string idlist = GetSelIDlist();
            if (idlist.Trim().Length == 0)
                return;
            int i = bll.DeleteList(idlist);
            if (i > 0)
            {
                lblMessage.Text = "删除数据成功，共删除" + i.ToString() + "条";
                Bind_GV();
            }
            else
            {
                lblMessage.Text = "删除数据失败";
            }
            Bind_GV();
        }

        #region GridView

        /// <summary>绑定数据：GridView
        ///     <remarks>2010-12-08 河北大学信管团队</remarks>
        /// </summary>
        public void Bind_GV()
        {
            IMIS_ERP.BLL.Pub_GetData bsns = new IMIS_ERP.BLL.Pub_GetData();
            Model.Pub_GetData model = new Model.Pub_GetData();
            //需要查询的表
            model.TableName = "Sale_Fee";
            //作为分页的字段
            model.PrimaryKey = "sfID";
            model.SortType = 3;
            model.Order = "sfIsRead asc,sfTime desc";
            //查询条件
            model.Condition = ViewState["condition"].ToString() ;
            //当前页
            model.PageIndex = Pagination1.PageCurrent;
            lblPageNow.Text = Pagination1.PageCurrent.ToString();
            //获得数据s
            DataSet ds = bsns.GetPage(model);
            //取得总页数
            Pagination1.PageSum = Convert.ToInt32(ds.Tables[1].Rows[0]["pageSum"]);
            lblPageCount.Text = ds.Tables[1].Rows[0]["pageSum"].ToString();
            lblRecordCount.Text = ds.Tables[1].Rows[0]["recordSum"].ToString();
            //为GridView绑定赋予数据源
            GV.DataSource = ds.Tables[0].DefaultView;
            //为GridView绑定数据
            GV.DataBind();
        }

        /// <summary>响应GridView事件：创建数据行时
        /// 	<remark>2010-12-08 BY 河北大学信管团队</remark>
        /// </summary>
        protected void GV_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //为数据行增加鼠标移动到行特性
                e.Row.Attributes.Add("onmouseover", "c=style.backgroundColor;this.style.backgroundColor='" + IMIS_ERP.BLL.Assistant.getColor1 + "'");
                //为数据行增加鼠标离开行特性
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
        }

        /// <summary>响应GridView事件：切换页时
        /// 	<remark>2010-12-08 BY 河北大学信管团队</remark>
        /// </summary>
        protected void GV_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GV.PageIndex = e.NewPageIndex;
            Bind_GV();
        }

        /// <summary>响应GridView事件：删除记录时
        /// 	<remark>2010-12-03 BY 河北大学信管团队</remark>
        /// </summary>
        protected void GV_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            if (bll.Delete(GV.DataKeys[e.RowIndex][0].ToString()))
            {
                lblMessage.Text = "删除数据成功";
                Bind_GV();
            }
            else
            {
                lblMessage.Text = "删除数据失败";
            }
        }

        /// <summary>绑定数据：获取需要删除的列
        ///     <remarks>2010-12-08 河北大学信管团队</remarks>
        /// </summary>
        private string GetSelIDlist()
        {
            string idlist = "";
            bool BxsChkd = false;
            for (int i = 0; i < GV.Rows.Count; i++)
            {
                CheckBox ChkBxItem = (CheckBox)GV.Rows[i].FindControl("ckbDel");
                if (ChkBxItem != null && ChkBxItem.Checked)
                {
                    BxsChkd = true;
                    //#warning 代码生成警告：请检查确认Cells的列索引是否正确
                    if (GV.DataKeys[i].Value != null)
                    {
                        idlist += GV.DataKeys[i].Value.ToString() + ",";
                    }
                }
            }
            if (BxsChkd)
            {
                idlist = idlist.Substring(0, idlist.LastIndexOf(","));
            }
            return idlist;
        }

        #endregion

        /// <summary>单击复选框事件：全选
        ///     <remarks>2010-12-08 河北大学信管团队</remarks>
        /// </summary>
        protected void ckbAll_CheckedChanged(object sender, EventArgs e)
        {

            bool isCheck = false;
            if (ckbAll.Checked)
            {
                isCheck = true;
            }

            ((CheckBox)GV.HeaderRow.FindControl("ckbAll")).Checked = isCheck;
            for (int i = 0; i < GV.Rows.Count; i++)
            {
                CheckBox ChkBxItem = (CheckBox)GV.Rows[i].FindControl("ckbDel");
                if (ChkBxItem != null)
                {
                    ChkBxItem.Checked = isCheck;
                }
            }


        }

        /// <summary>单击复选框事件：GridView头部全选
        ///     <remarks>2010-12-08 河北大学信管团队</remarks>
        /// </summary>
        protected void ckbAll_CheckedChanged1(object sender, EventArgs e)
        {
            bool isCheck = false;
            if (((CheckBox)GV.HeaderRow.FindControl("ckbAll")).Checked)
            {
                isCheck = true;
            }
            ckbAll.Checked = isCheck;
            for (int i = 0; i < GV.Rows.Count; i++)
            {
                CheckBox ChkBxItem = (CheckBox)GV.Rows[i].FindControl("ckbDel");
                if (ChkBxItem != null)
                {
                    ChkBxItem.Checked = isCheck;
                }
            }
        }

    }
}